<template>
	<el-container v-if="isAlready">
		<el-header  style="padding-top: 0px;padding-bottom: 0px;height: 60px;background-color: rgb(249, 249, 249);border-bottom: 1px solid #e6e6e6;">
			<div class="left-panel">
				<div style="margin-left: -5px;float: left;">
					<img class="topblockimg" src="img/pandian-icon.png"/>
				</div>
				<label style="font-size: 14px;margin-left: 10px;">盘点单</label>
			</div>
			<div class="right-panel">
				<fsButton ref="button" plain size="small" :buttons="buttons" @clickHandler="buttonHandler"></fsButton>
			</div>
		</el-header>
		<div style="background-color: #fff;width: 100%;">
			<el-descriptions class="margin-top stockdescriptions" :column="3" border>
				<el-descriptions-item label="盘点单编号:"  label-class-name="checklabelwidth">{{documentData.code}}</el-descriptions-item>
				<el-descriptions-item label="盘点企业:" label-class-name="checklabelwidth"><ex-label :key="documentData.orgId" code="SysOrganization" :value="documentData.orgId"/></el-descriptions-item>
				<el-descriptions-item label="制定人:" label-class-name="checklabelwidth"><ex-label :key="documentData.createUser" code="SysUser" :value="documentData.createUser"/></el-descriptions-item>
				<el-descriptions-item label="制定日期:" label-class-name="checklabelwidth"><ex-label :key="documentData.createTime" format code="DATETIME" :value="documentData.createTime"/></el-descriptions-item>
				<el-descriptions-item label="盘点任务:" label-class-name="checklabelwidth">{{taskData.name}}</el-descriptions-item>
				<el-descriptions-item label="盘点单名称:" label-class-name="checklabelwidth">{{documentData.name}}</el-descriptions-item>
				
			</el-descriptions>
		</div>
		<el-main style="padding: 0px;margin-top: 5px;background-color: #fff;">
			<div style="height: 100%">
				<el-container>
					<el-header style="width: 98%;height: 60px;padding: 0px 15px;border: 1px rgba(0, 0, 0, .08) solid;
				margin-left: 1%;background-color: rgb(249, 249, 249);">
						<div class="left-panel">
					
							<sc-select-filter-check :data="filterData" :label-width=110 @on-change="tabChange">
							</sc-select-filter-check>
						</div>
						<div class="right-panel">
							<ex-search v-if="searches" ref="search" :searches="searches" @searchOk="searchHandler" default-code="assetCode" placeholder="请输入资产编码" :fixedParam=fixedParams></ex-search>
						</div>
					</el-header>
					<el-main class="nopadding">
						<ex-table id="checkDetailTable" ref="table" :columns="columns" :url="this.$API.asset.checkDetailApi.page.url" :refs="this.$refs" search="search" @columnClick="columnClickHandler" :minHeight="305"></ex-table>
					</el-main>
				</el-container>
			</div>
			<data-card ref="dataCard"></data-card>
<!--			<ex-selector ref="userSecter" code="SysUser" :model="form" prop="checkUserId"></ex-selector>-->
			<asset-selecter ref="assetSelecter" :ids="assetIds" @success="assetSelectHandler"></asset-selecter>
		</el-main>
	</el-container>
	<view-dialog ref="viewDialog"/>
	<import-check-detail ref="importCheckDetail" @success="refreshPageFun"></import-check-detail>
</template>

<script>
import ExTable from "@/components/exTable/index.vue";
import ExSearch from "@/components/exSearch/index.vue";
import ExTreeNavigation from "@/components/exSearchTree/index.vue";
import ExSearchTree from "@/components/exSearchTree/index.vue";
import ExLabel from "@/components/exLabel/index.vue";
import Selector from "@/components/exSelector/selecter.vue";
import ExSelector from "@/components/exSelector/index.vue";
import tool from "@/utils/tool";
import viewDialog from "@/views/asset/check/checkDocument/view.vue";
import AssetSelecter from "@/views/asset/asset/asset-selecter.vue";
import DataCard from "@/views/asset/card/card.vue";
import moment from "moment";
import router from "@/router";
import useTabs from "@/utils/useTabs";
import ImportCheckDetail from "@/views/asset/check/checkDocument/importCheckDetail.vue";
import scSelectFilterCheck from "@/components/scSelectFilterCheck/index.vue";
export default {
	name: '盘点单明细',
	components: {
		scSelectFilterCheck,
		ImportCheckDetail,
		DataCard,		
		AssetSelecter,
		ExSelector,
		ExLabel,
		ExSearchTree,
		ExTreeNavigation,
		ExSearch,
		ExTable,
		Selector,
		viewDialog,
	},
	data() {
		return {
			loading: false,
			currentTime: moment(new Date()).format("YYYY-MM-DD HH:mm:ss"),
			dialog: {
				save: false,
				permission: false,
			},
			isAlready: false,
			apiObj: [],
			buttons: [
				// {name: "添加明细", icon: "el-icon-plus", click: "addAsset", permission: ""},
				// {name: "删除", icon: "el-icon-delete",type: "info", click: "del", permission: ""},
				// {name: "导入", icon: "el-icon-upload",type: "info", click: "upload", permission: ""},
				// {name: "清空", icon: "el-icon-close",type: "info", click: "delete", permission: ""},
				{name: "盘点单详情", icon: "el-icon-search",type: "info", click: "viewData", permission: ""},
				// {name: "盘点人变更", icon: "el-icon-edit",type: "info", click: "changeUser", permission: ""},
			],
			searches: null,
			columns: [
				{code: "attachFiles", name: "拍照信息", width:"90",formatter: "IMAGE"},
				{code: "checkResult", name: "盘点结果", width:"100",formatter:"DICT",key: "CHECK_RESULT"},
				{code: "isPhoto", name: "是否必须拍照", width:"110",formatter:"DICT",key: "IS_PHOTO"},
				{code: "checkUser", name: "盘点人", width:"100",formatter: "DICT",key: "SysUser"},
				{code: "realityCheckUser", name: "实际盘点人", width:"110",formatter: "DICT",key: "SysUser"},
				{code: "assetCount", name: "数量", width:"100"},
				{code: "checkAccount", name: "盘点数量", width:"100"},
				{code: "checkDate", name: "盘点时间", width:"150",formatter: "DATE"},
				{code: "assetCode", name: "资产编码", width:"160",click:"show1"},
				{code: "ncCode", name: "财务编码", width:"170"},
				{code: "assetCategory", name: "资产类别", width:"220",formatter:"DICT",key: "FamsAssetCategory"},
				{code: "assetName", name: "资产名称", width:"200"},
				{code: "financialCode", name: "财务类型",width: "85", formatter: function(row){
						if(row.ncCode){
							return "账内";
						}
						return "账外";
					}},
				{code: "model", name: "规格型号", width:"120"},
				{code: "originalValue", name: "资产原值", width:"120"},
				{code: "netValue", name: "资产净值", width:"120"},
				{code: "usingUser", name: "使用人", width:"100",formatter: "DICT",key: "SysUser"},
				{code: "usingOrg", name: "使用部门", width:"210",formatter: "DICT",key: "SysOrganization"},
				{code: "realityUsingUser", name: "实际使用人", width:"150",formatter: "DICT",key: "SysUser"},
				{code: "checkPosition", name: "盘点位置", width:"100",formatter: "DICT",key: "FamsPosition"},
				{code: "assetPosition", name: "资产位置", width:"100",formatter: "DICT",key: "FamsPosition"},
				{code: "checkDescription", name: "盘点备注说明", width:"300"},
			],
			filterData: [
				{
					title: "【盘点结果汇总】",
					key: "checkResult",
					multiple: false,
					options: [
						{
							label: "全部(0)",
							value: ""
						},
						{
							label: "未盘(0)",
							value: "0"
						},
						{
							label: "正常(0)",
							value: "1"
						},
						{
							label: "异常(0)",
							value: "2"
						},
						{
							label: "盘盈(0)",
							value: "3"
						},
						{
							label: "盘亏(0)",
							value: "4"
						},
					]
				},
			],
			fixedParams: {
			},
			readDeptIds: this.$TOOL.current.readDeptIds(),
			searchParams:{},
			checkResult: '',
			parentLevelCode:"",
			filterAssetIds: [],
			assetIds:[],
			taskId: '',
			documentId: '',
			taskData: {},
			documentData: {},
			assetCategoryIds: [],
			ids: [],
			resultDatas: {},
			loginUser: tool.current.user(),
			updateDetailIds:[],
			minioServerUrl: '',
		}
	},
	async created() {
		//如果是通过跳转过来的，标签的名称设置为跳转后的标题
		let route = router.currentRoute.value;
		if (route.meta) {
			this.title = route.meta.title;
		}
		let routeParams = this.$store.state.routeParams[this.$route.params.key];
		if (routeParams) {
			if (routeParams.title) {
				useTabs.setTitle(routeParams.title);
				document.title = routeParams.title;
			}
			if (routeParams.fixedParams) {
				if (routeParams.fixedParams.taskId) {
					this.taskId = routeParams.fixedParams.taskId;
					this.fixedParams.taskId = routeParams.fixedParams.taskId;
				}
				if (routeParams.fixedParams.documentId) {
					this.documentId = routeParams.fixedParams.documentId;
					this.fixedParams.documentId = routeParams.fixedParams.documentId;
				}
			}
			if (routeParams.searchParams) {
				Object.assign(this.searchParams, routeParams.searchParams);
			}
		}
		this.fixedParams.queryUser = this.loginUser.id;
		this.fixedParams.readDeptIds = this.readDeptIds;
		if (this.documentId) {
			let queryParams = {};
			if(routeParams){
				Object.assign(queryParams,this.fixedParams);
			}
			await this.getCountData(queryParams);
		}
		let queryDtoDept = {
			firstEntity: "FamsAssetCheckTask",
			firstEntityId: this.taskId,
			secondEntity: "FamsAssetCategory",
		};
		let assetCategoryRsp = await this.$API.system.relMap.load.post(queryDtoDept);
		if (assetCategoryRsp.code === 2000 && Array.isArray(assetCategoryRsp.data)) {
			this.assetCategoryIds = assetCategoryRsp.data;
		}
		await this.getQueryDataByQueryDto();
		this.searches = [
			{type: "selector", name: "盘点人", code: "checkUser", placeholder: "请选择盘点人", dictCode:'SysUser',priKey:true},
			{type: "selector", name: "实际盘点人", code: "realityCheckUser", placeholder: "请选择实际盘点人", dictCode:'SysUser',priKey:true},
			{type: "input", name: "资产编码", code: "assetCode", placeholder: "请输入资产编码"},
			{type: "input", name: "财务编码", code: "ncCode", placeholder: "请输入财务编码"},
			{type: "input", name: "资产名称", code: "assetName", placeholder: "请输入资产名称"},
			{type: "tree-select", name: "资产类别", code: "assetCategoryIds", placeholder: "请选择资产类别", dictCode:'FamsAssetCategory', checkStrictly:true,multiple:true,dataPermissionProcess:true,permissionData:this.assetCategoryIds},
			{type: "select", name: "财务类型", code: "assetType", placeholder: "请选择财务类型", dictCode:'FINANCIAL_TYPE'},
			{type: "tree-select", name: "资产位置", code: "assetPositionIds", placeholder: "请选择资产位置", dictCode:'FamsPosition', checkStrictly:true,multiple:true},
			{type: "select", name: "是否拍照", code: "isPhoto", placeholder: "请选择是否拍照", dictCode:'IS_PHOTO'},
		]
		this.isAlready = true;
	},
	async mounted() {
		this.minioServerUrl = await this.$TOOL.minioServerUrl();
	},
	beforeCreate() {
	},
	methods: {
		buttonHandler(index, name) {
			this.$options.methods[name](this, index);
		},
		async searchHandler() {
			let queryParams = {pathCode: this.parentLevelCode}
			await this.$refs.table.refresh(queryParams);
		},
		columnClickHandler(name,data){
			this.$options.methods[name](this, data);
		},
		//刷新页面
		refreshPageFun() {
			this.searchHandler();
			let queryParams = {};
			Object.assign(queryParams,this.fixedParams);
			this.getCountData(queryParams);
		},
		async getQueryDataByQueryDto() {
			let queryDtoData = {};
			queryDtoData.id = this.documentId;
			queryDtoData.taskId = this.taskId;
			this.taskData = {};
			this.documentData = {};
			let rsp = await this.$API.asset.checkDocumentApi.getQueryDataByQueryDto.post(queryDtoData);
			if (rsp.code === 2000 && rsp.data) {
				if (rsp.data.assetCheckTask) {
					this.taskData = rsp.data.assetCheckTask;
				}
				if (rsp.data.assetCheckDocument) {
					this.documentData = rsp.data.assetCheckDocument;
				}
			}
		},
		async getCountData(queryParams) {
			let rsp = await this.$API.asset.checkDetailApi.countResultByQueryDto.post(queryParams);
			if (rsp.code === 2000) {
				if (rsp.data != null) {
					let optionsData = [
						{
							label: "全部(" + rsp.data.totalCount + ")",
							value: ""
						},
						{
							label: "未盘(" + rsp.data.nodoCount + ")",
							value: "0"
						},
						{
							label: "正常(" + rsp.data.normalCount + ")",
							value: "1"
						},
						{
							label: "异常(" + rsp.data.errCount + ")",
							value: "2"
						},
						{
							label: "盘盈(" + rsp.data.surplusCount + ")",
							value: "3"
						},
						{
							label: "盘亏(" + rsp.data.lossCount + ")",
							value: "4"
						},
					];
					Object.assign(this.filterData[0].options, optionsData);
				}
			}
		},
		//查看
		async show1(_this, row,index) {
			_this.$refs.button.loading(index, true);
			let assetCategoryData = await _this.$cache.data.loadAssetCategory(row.assetCategory);
			try {
				let entity = {cardId: assetCategoryData.assetCardId, _editFlag: false}
				_this.$nextTick(async () => {
					let assetRsp = await _this.$API.asset.asset.findById.post({id:row.assetId});
					if(assetRsp.code === 2000 && assetRsp.data){
						_this.$refs.button.loading(index, false);
						_this.$refs.dataCard.open(Object.assign(entity, assetRsp.data));
					}else{
						_this.$refs.button.loading(index, false);
						_this.$message.message("未查询到资产信息");
					}
				});
			} catch {
				_this.$refs.button.loading(index, false);
			}
		},
		viewData(_this,index){
			_this.$refs.button.loading(index, true);
			if(_this.documentId){
				_this.$nextTick(() => {
					let entity = {
						taskId: _this.taskData.id,
						taskName: _this.taskData.name,
						taskCode: _this.taskData.code,
						documentId: _this.documentData.documentId,
						documentCode: _this.documentData.code,
						documentName: _this.documentData.name,
						startDate: _this.taskData.startDate,
						endDate: _this.taskData.endDate,
						checkManageUsers: _this.documentData.checkManageUser,
						documentCreateTime: _this.documentData.createTime,
						documentCreateUser: _this.documentData.createUser,
						documentDescription: _this.documentData.description,
						documentOrgId: _this.documentData.orgId,
					}
					_this.$refs.viewDialog.open(entity);
					_this.$refs.button.loading(index, false);
				})
			}else{
				_this.$refs.button.loading(index, false);
				_this.$message.error("请先保存盘点单信息");
				return;
			}
		},
		//标签切换
		async tabChange(name){
			name.pathCode = this.parentLevelCode
			await this.$refs.table.refresh(name);
		},
	}
}

</script>
<style  scoped>
:deep(.labelClassName){
	color: red !important;
}

:deep(.checklabelwidth){
	width:100px;
}

</style>
